package entities;

import exceptions.NonexistentEntityException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author sha
 */
public class PropinsiModel {

    public PropinsiModel() {
        emf = Persistence.createEntityManagerFactory("CeritaUnyilPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public List<Propinsi> getPropinsi() {
        List<Propinsi> propinsi = new ArrayList<Propinsi>();

        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT a FROM Propinsi AS a");
            propinsi = q.getResultList();

        } finally {
            em.close();
        }
        return propinsi;
    }

    public Propinsi findPropinsi(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Propinsi.class, id);
        } finally {
            em.close();
        }
    }

    public void rekamPropinsi(Propinsi propinsi) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(propinsi);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void updatePropinsi(Propinsi propinsi) {
        EntityManager em = getEntityManager();
        try {
            em.getTransaction().begin();
            em.merge(propinsi);
            em.getTransaction().commit();
        } finally {
            em.close();
        }
    }

    public void deletePropinsi(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Propinsi propinsi;
            try {
                propinsi = em.getReference(Propinsi.class, id);
                propinsi.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The user with id " + id + " no longer exists.", enfe);
            }
            em.remove(propinsi);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public Propinsi findPropinsi(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Propinsi.class, id);
        } finally {
            em.close();
        }
    }

    public void edit(Propinsi propinsi) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            propinsi = em.merge(propinsi);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(long idpropinsi) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Propinsi propinsi;
            try {
                propinsi = em.getReference(Propinsi.class, idpropinsi);
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("user belum dipilih.", enfe);
            }
            em.remove(propinsi);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
}